package com.wskj.shiro.chapter3;

import java.util.Arrays;

import org.apache.shiro.authz.UnauthorizedException;
import org.apache.shiro.subject.Subject;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;

public class RoleTest extends BaseTest {
	
	@Test
	public void  testHasRole() {

		login("classpath:role/shiro-role.ini", "zhang","123");
		Subject subject = subject();
		
		//判断是否拥有角色 role1		 
	    Assert.assertTrue(subject.hasRole("role1"));  
	    
	    //判断拥有角色：role1 and role2  
	    Assert.assertTrue(subject.hasAllRoles(Arrays.asList("role1", "role2")));  
	    //判断拥有角色：role1 and role2 and !role3  
	    boolean[] result = subject.hasRoles(Arrays.asList("role1", "role2", "role3"));  
	    Assert.assertEquals(true, result[0]);  
	    Assert.assertEquals(true, result[1]);  
	    // 没有角色 role3
	    Assert.assertEquals(false, result[2]);  		
	}
	
	
	/*@Test(expected = UnauthorizedException.class)*/
    public void testCheckRole() {
        login("classpath:role/shiro-role.ini", "zhang", "123");
        
        //断言拥有角色：role1
        subject().checkRole("role1");
        //断言拥有角色：role1 and role3 失败抛出异常
        subject().checkRoles("role1", "role3");
    }

}
